package barcan.flutter.io.image_picker;

import android.app.Activity;
import android.content.Intent;
import android.util.TypedValue;

import java.util.ArrayList;

import barcan.flutter.io.image_picker.bean.ImageItem;
import barcan.flutter.io.image_picker.ui.ImageGridActivity;
import barcan.flutter.io.image_picker.view.CropImageView;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * ImagePickerPlugin
 */
public class ImagePickerPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener{
    /**
     * Plugin registration.
     */
    private Activity activity;
    private Result result;
   private  int maxCount=1;
    private ImagePickerPlugin(Activity activity) {
        this.activity = activity;
    }

    public static void registerWith(Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), "image_picker");
        ImagePickerPlugin imagePickerPlugin = new ImagePickerPlugin(registrar.activity());
        channel.setMethodCallHandler(imagePickerPlugin);
        registrar.addActivityResultListener(imagePickerPlugin);
    }
    private  void initImagePicker(boolean isHead) {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setShowCamera(false);
        if (isHead) {
            ImagePicker.getInstance().setSelectLimit(1);
            ImagePicker.getInstance().setSaveRectangle(false);
            ImagePicker.getInstance().setStyle(CropImageView.Style.CIRCLE);
            ImagePicker.getInstance().setCrop(true);
            ImagePicker.getInstance().setMultiMode(false);
            Integer radius = Integer.valueOf(100);
            radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, activity.getResources().getDisplayMetrics());
            ImagePicker.getInstance().setFocusWidth(radius * 2);
            ImagePicker.getInstance().setFocusHeight(radius * 2);
        } else {
            imagePicker.setCrop(false);
            imagePicker.setSaveRectangle(true);
            ImagePicker.getInstance().setStyle(CropImageView.Style.RECTANGLE);
            ImagePicker.getInstance().setSelectLimit(maxCount);
            if(maxCount==1){
                imagePicker.setMultiMode(false);
            }else {
                imagePicker.setMultiMode(true);
            }
            imagePicker.setOutPutX(800);
            imagePicker.setOutPutY(800);
            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, activity.getResources().getDisplayMetrics());
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, activity.getResources().getDisplayMetrics());
            imagePicker.setFocusWidth(width);
            imagePicker.setFocusHeight(height);
        }
    }

    private void pickView(boolean isCamera, String color,boolean isEnglishLanguage) {
        Intent intent = new Intent(activity, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, isCamera); // 是否是直接打开相机
        intent.putExtra(ImageGridActivity.COLOR_THEME,color);
        intent.putExtra(ImageGridActivity.ENGLISH_THEME,isEnglishLanguage);
       activity.startActivityForResult(intent, ImagePicker.REQUEST_CODE_TAKE);
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == ImageGridActivity.RESULT_CODE) {
            if (data != null) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                String scanResult="";
                if(images!=null){
                    scanResult=images.get(0).path;
                }
                //将扫描出的信息显示出来
                if (result != null) {
                    result.success(scanResult);
                    return true;
                }
            }


        } else if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                String scanResult="";
                if(images!=null){
                    scanResult=images.get(0).path;
                }
                //将扫描出的信息显示出来
                if (result != null) {
                    result.success(scanResult);
                    return true;
                }
            }
        } else {
            if (result != null)
                result.error("error", null, null);
            return  false;
        }
        return false;
    }

    @Override
    public void onMethodCall(MethodCall call, Result result) {
        if (call.method.equals("getPlatformVersion")) {
            result.success("Android " + android.os.Build.VERSION.RELEASE);
        }
        if (call.method.equals("pickImage")) {
            this.result = result;
            boolean isHead = call.argument("isHead");
            int  source= call.argument("source");
            String color =call.argument("color");
            boolean isEnglishLanguage=false;
            if(call.argument("isEnglishLanguage")==null){
                isEnglishLanguage=false;
            }else {
                isEnglishLanguage=call.argument("isEnglishLanguage");
            }
            if(call.argument("maxCount")==null){
                maxCount=1;
            }else {
                maxCount=call.argument("maxCount");
            }
            initImagePicker(isHead);
            pickView(source==0,color,isEnglishLanguage);
            return;
        }
    }
}
